package stack_queue.leetcode;

import java.util.Stack;

/**
 * 双栈实现队列
 */
public class MyQueue {
    // s1存储具体元素
    private Stack<Integer> s1;
    // s2作为辅助
    private Stack<Integer> s2;
    public MyQueue() {
        s1 = new Stack<>();
        s2 = new Stack<>();
    }
    public void push(int x) {
        if (s1.isEmpty()) {
            s1.push(x);
        }else {
            while (!s1.isEmpty()) {
                s2.push(s1.pop());
            }
            // 直接将队尾元素入s1
            s1.push(x);
            // 将s2的所有元素依次出栈再入s1
            while (!s2.isEmpty()) {
                s1.push(s2.pop());
            }
        }
    }

    public int pop() {
        return s1.pop();
    }

    public int peek() {
        return s1.peek();
    }

    public boolean empty() {
        return s1.isEmpty();
    }
}
